<script>
	var url;
	$(document).ready(function(){
	
		newData = function (){
			$('#dialog').dialog({
				title: 'Tambah Data Keluarga',
				width: 550,
				height: $(window).height()*0.97,
				closed: true,
				cache: false,
				href: base_url+'daftar_keluarga/add',
				modal: true
			});
			 
			$('#dialog').dialog('open');
			url = base_url+'daftar_keluarga/save';
		}

		//eksport ke Excel
		exportExcel = function (){
			url = base_url+'daftar_keluarga/export';
		}

		editData = function (value){
			//var row = $('#dg').datagrid('getSelected');	
			//if (row){
				$('#dialog').dialog({
					title: 'Edit Data Keluarga',
					width: 550,
					height: $(window).height()*0.97,
					closed: true,
					cache: false,
					href: base_url+'daftar_keluarga/edit/'+value,
					modal: true
				});
				
				$('#dialog').dialog('open');  
				url = base_url+'daftar_keluarga/save/edit';
			//}
		}

		// end newData
		detailData = function (value){
			//var row = $('#dg').datagrid('getSelected');
			//if (row){
				$('#dialog').dialog({
					title: 'Detail Data Keluarga',
					width: 550,
					height: 550,
					closed: true,
					cache: false,
					href: base_url+'daftar_keluarga/detail/'+value,
					modal: true
				});
				
				$('#dialog').dialog('open');  
				url = base_url+'daftar_keluarga/detail';
			//}
		}
		//end detailData
		
		deleteData = function (val){
			//var row = $('#dg').datagrid('getSelected');
			//if(row){
				if(confirm("Apakah yakin akan menghapus data '" + val + "'?")){
					var response = '';
					$.ajax({ type: "GET",
						 url: base_url+'mahasiswa/delete/' + val,
						 async: false,
						 success : function(response){
							var response = eval('('+response+')');
							if (response.success){
								$.messager.show({
									title: 'Success',
									msg: 'Data Berhasil Dihapus'
								});
								// reload and close tab
								$('#dg').datagrid('reload');
							} else {
								$.messager.show({
									title: 'Error',
									msg: response.msg
								});
							}
						 }
					});
				}
			//}
		}
		//end deleteData 
		
		saveData = function(){
			//alert('tada : '+url)
			// get data anggota dari grid
			var str = $('#dg_anggota_keluarga').datagrid('getData');
			$('#anggota_keluarga').val(JSON.stringify(str));

			$('#form1').form('submit',{
				url: url,
				onSubmit: function(){
					return $(this).form('validate');
				},
				success: function(result){
				console.log(result);
				// return 1;
				var result = eval('('+result+')');
				if (result.success){
					$('#dialog').dialog('close');		// close the dialog
					$('#dg').datagrid('reload');		// close the dialog
					//print pdf
					// result.no_surat
					//print_skp(result.no_surat);
				} else {
					$.messager.show({
						title: 'Error',
						msg: result.msg
					});
				}
			}
			});
		}
		//end saveData
		
		actionbutton = function(value, row, index){
			var col;
			//if (row.kd_fakultas != null) {
				col = '<a href="#" onclick="detailData(\''+row.id_keluarga+'\');" class="easyui-linkbutton" iconCls="icon-edit">Detail</a>';
				col += '&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#" onclick="editData(\''+row.id_keluarga+'\');" class="easyui-linkbutton" iconCls="icon-edit">Tambah Anggota</a>';
			//}
			return col;
		}
		
		// load grid
		$(function(){
			$('#dg').datagrid({url:"<?=base_url()?>daftar_keluarga/grid"});	
			//$('#dg').datagrid('enableFilter'); 			
		});	
		
		
		// filter
		filter = function(){
			$('#dg').datagrid('load',{
				id_keluarga : $('#s_id_keluarga').val(),
				dusun : $('#s_dusun').val(),
				rt : $('#s_rt').val(),
				rw : $('#s_rw').val(),
				ekonomi : $('#s_ekonomi').val(),
			});
			//$('#dg').datagrid('enableFilter');
		}
		
		//# Tombol Bawah
		$(function(){
			var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
				buttons:[
					{
						iconCls:'icon-add',
						text:'Tambah Data',
						handler:function(){
							newData();
						}
					},
				
					{
						iconCls:'icon-data-kelompok-matakuliah',
						text:'Export Excel',
						handler:function(){
							window.open('<?=base_url().'daftar_keluarga/exportExcel'?>');
						}
					}
				]
			});			
		});
		
	});
</script>

<table id="dg" title="" data-options="
			rownumbers:true,
			singleSelect:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:30,
			fit:true,
			toolbar:'#toolbar',
			">
	<thead>
		<tr>
			<!--<th field="tahun_penerimaan" sortable="true" width="80">Tahun Penerimaan</th> -->
			<th field="id_keluarga" sortable="true" width="125">No. KK</th>
			<th field="kepala_keluarga" sortable="true" width="125">No.KTP Kepala Kel</th>
			<th field="nama" sortable="true" width="125">Kepala Keluarga</th>
			<th field="alamat" sortable="true" width="150">Alamat</th>
			<th field="nama_desa" sortable="true" width="100">Dusun</th>
			<th field="rt" sortable="true" width="80">RT</th>
			<th field="rw" sortable="true" width="80">RW</th>
			<th field="ekonomi" sortable="true" width="100">Ekonomi</th>
			<th field="action" width="200" align="center" formatter="actionbutton" width="100">Aksi</th>
		</tr>
	</thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
	<div style="margin-bottom:5px">
		
	</div>
	<div>
		<table width="100%" border="0">
		    <tr>
				<td>No. KK</td>
				<td>: 
					<input name="s_id_keluarga" id="s_id_keluarga" size="15">
				</td>
				<td>&nbsp;</td>
				<td>Dusun</td>
				<td>: 
					<select id="s_dusun" name="s_dusun">
						<?php echo $this->mdl_prosedur->option_desa(); ?>	
					</select></li>
				</td>
				<td>&nbsp;</td>
				<td>Ekonomi</td>
				<td>: 
					<select id="s_ekonomi" name="s_ekonomi" style="width:190px;">
						<option value="">-Pilih-</option>
						<option value="Kaya">Kaya</option>
						<option value="Mampu">Mampu</option>
						<option value="Cukup">Cukup</option>
						<option value="Kurang Mampu">Kurang Mampu</option>
						<option value="Miskin">Miskin</option>
					</select>
				</td>
			</tr>
			<tr>	
				<td>RT</td>
				<td>: 
					<input name="s_rt" id="s_rt" size="10">
				</td>
				<td>&nbsp;</td>
				<td>RW</td>
				<td>: 
					<input name="s_rw" id="s_rw" size="10">
				</td>
		    </tr>		
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;&nbsp;<a href="#" onclick="filter()" class="easyui-linkbutton btn" iconCls="icon-search">Cari</a></td>
			</tr>
		</table>
	</div>
</div>
